<?php

require_once 'EZend/Controller/AdminBaseController.php';

class Admin_ProfileController extends AdminBaseController {
	public function indexAction() {
		$this->_helper->layout->setLayout('iframe');
	}
	
	public function modifyAction() {
		$this->_helper->layout->setLayout('iframe');
		$data = $this->getCurrentAdminUser()->getUserInfo();
		$this->view->username = $data['user_name'];
		
		if ($this->getRequest()->getPost('action') == 'modify') {
			$oldpwd = trim((string) $this->getRequest()->getPost('oldpwd'));
			$newpwd = trim((string) $this->getRequest()->getPost('newpwd'));
			$checkPwd = trim((string) $this->getRequest()->getPost('check_pwd'));
			if ($oldpwd == '' || $newpwd == '' || $checkPwd == '') return false;
			if ($newpwd != $checkPwd) return $this->showNotice('前后密码不一致');
			if (!preg_match('/^[0-9A-Za-z]{5,20}$/', $newpwd)) return $this->showNotice('密码不规范');
			if (md5($oldpwd) != $data['password']) return $this->showNotice('旧密码错误');
			
			if (!$this->getAdminUserService()->update($data['user_id'], array('password' => $newpwd))) return $this->showNotice('修改密码失败');
			return $this->showNotice('修改密码成功', $this->getInnerUrl(array('action' => 'modify')));
		}
	}
	
	protected function getAdminUserService() {
		require_once 'Models/Admin/Service/AdminServiceFactory.php';
		return AdminServiceFactory::getInstance()->createAdminUserService();
	}
}